package com.soft.collateral.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.Agencies;
import com.soft.collateral.entity.Contracts;
import com.soft.collateral.entity.Edger;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.ContractsPDFService;
import com.soft.collateral.service.ContractsService;
import com.soft.collateral.service.FileService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * 合同表 前端控制器
 * </p>
 *
 * @author liubin
 * @since 2025-07-25
 */
@Slf4j
@Api(tags = "后台系统-合同接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/contracts")
public class ContractsController {

    private final ContractsService contractsService;
    private final ContractsPDFService contractsPDFService;

    @ApiOperation("分页查询合同")
    @PostMapping("page")
    public Result page(@RequestBody PageQueryVO vo) {

        Page<Contracts> ontractsPage = contractsService.page(vo);
        if(ontractsPage!=null && ontractsPage.getTotal()>0){
            return new Result(true , MessageConstant.GET_CONTRACTS_SUCCESS , ontractsPage);
        }
        return new Result(false , MessageConstant.GET_CONTRACTS_FAIL);
    }

    @ApiOperation("新增合同")
    @PostMapping("create")
    public Result create(@RequestBody Contracts contracts) {

        boolean flag = contractsService.saveContracts(contracts);
        if(flag){
            return new Result(true ,MessageConstant.SAVE_CONTRACTS_SUCCESS);
        }
        return new Result(false ,MessageConstant.SAVE_CONTRACTS_FAIL);

    }

    @ApiOperation("根据id查询合同")
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {

        Contracts contracts = contractsService.getById(id);
        if(contracts!=null){
            return new Result(true , MessageConstant.GET_CONTRACTS_SUCCESS , contracts);
        }
        return new Result(false , MessageConstant.GET_CONTRACTS_FAIL);

    }

    @ApiOperation("批量获取合同")
    @PostMapping("batch")
    public Result getContractsByIds(@RequestBody Map<String, List<Integer>> params) {
        List<Integer> ids = params.get("ids");
        if (ids == null || ids.isEmpty()) {
            return new Result(false, "参数错误");
        }

        List<Contracts> contracts = contractsService.listByIds(ids);
        return new Result(true, MessageConstant.GET_CONTRACTS_SUCCESS, contracts);
    }

    @ApiOperation("根据id删除合同")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {

        boolean flag = contractsService.removeById(id);
        if(flag){
            return new Result(true ,MessageConstant.DELETE_CONTRACTS_SUCCESS);
        }
        return new Result(false ,MessageConstant.DELETE_CONTRACTS_FAIL);
    }

    @ApiOperation("获取合同类型和付款方式枚举")
    @GetMapping("enums")
    public Result getEnums() {
        Map<String, Map<Integer, String>> enums = new HashMap<>();

        // 合同类型
        Map<Integer, String> contractTypes = new HashMap<>();
        contractTypes.put(0, "正常售出");
        contractTypes.put(1, "拍卖售出");
        enums.put("contractTypes", contractTypes);

        // 付款方式
        Map<Integer, String> paymentMethods = new HashMap<>();
        paymentMethods.put(0, "一次付清");
        paymentMethods.put(1, "分期付款");
        enums.put("paymentMethods", paymentMethods);

        return new Result(true, MessageConstant.GET_ENUMS_SUCCESS, enums);
    }


    @ApiOperation("生成合同PDF")
    @PostMapping("generate-pdf")
    public Result generatePdf(@RequestBody Contracts contracts) {
        try {
            byte[] pdfBytes = contractsPDFService.generateContractPDF(contracts);
            String base64Pdf = Base64.getEncoder().encodeToString(pdfBytes);
            return new Result(true, MessageConstant.SAVE_PDF_SUCCESS, base64Pdf);
        } catch (Exception e) {
            return new Result(false, MessageConstant.SAVE_PDF_FAIL);
        }
    }

}
